package com.key.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.key.util.InfoText;
import com.key.util.InfoText.BuildingInfo;
import com.key.util.Util;
import com.key.wanderbupt.R;

public class KeySearchResultActivity extends Activity {
	
	private int pos = -1;
	
	private TextView title = null, info = null;
	
	private ImageView image = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.searchresult);
		
		initTextView();
		
		Bundle bundle = new Bundle();
        bundle = getIntent().getExtras();  
        pos = Integer.parseInt(bundle.getString("pos"));
        
        if( pos < 0 || pos > InfoText.BuildingInfo.size() ) {
        	Util.toastPopWindow(this, "无获取到信息");
			Intent gameIntent = new Intent(this,KeySearchDataActivity.class);
			this.startActivity(gameIntent);
			this.finish();	
        }
        
        if( setTextView() == false ) {
        	Util.toastPopWindow(this, "无获取到信息");
			Intent gameIntent = new Intent(this,KeySearchDataActivity.class);
			this.startActivity(gameIntent);
			this.finish();	
        }
	}
	
	private void initTextView() {
		
		title = (TextView) findViewById(R.id.buildingtitle);
		info = (TextView) findViewById(R.id.buildinginfo);
		image = (ImageView) findViewById(R.id.buildingimage);
	}
	
	private boolean setTextView() {
		
		BuildingInfo bi = InfoText.BuildingInfo.get(Util.result[pos]);
		if( bi == null ) {
			return false;
		}
		title.setText((String)Util.result[pos]);
		info.setText((String)bi.BuildingInfos);
		readBitmap(bi.BuildingImage);
		
		return true;
	}
	
	private void readBitmap( int id ) {
		
		Resources res = getResources();
		Bitmap bitmap = BitmapFactory.decodeResource(res,id);
		image.setImageBitmap(bitmap);
	}
}
